package palmtop.com.palmtop.activity;

import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.widget.Button;
import android.widget.EditText;

import net.grandcentrix.tray.AppPreferences;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import palmtop.com.palmtop.R;
import palmtop.com.palmtop.util.Config;
import palmtop.com.palmtop.util.NetUtil;
import palmtop.com.palmtop.util.ToastUtil;

public class ModPsswordActivity extends BaseActivity {

    @BindView(R.id.oldpassword)
    TextInputLayout oldpassword;
    @BindView(R.id.newpassword)
    TextInputLayout newpassword;
    @BindView(R.id.confirmpassword)
    TextInputLayout confirmpassword;
    @BindView(R.id.yes)
    Button yes;
    String userid;
    @BindView(R.id.oldPassword)
    EditText oldPassword;
    @BindView(R.id.newPassword)
    EditText newPassword;
    @BindView(R.id.conPwd)
    EditText conPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mod_pssword, "修改密码", 1);
        MyApplication app = (MyApplication) getApplication();
        app.addActivityList(ModPsswordActivity.this);
        ButterKnife.bind(this);
        AppPreferences pref = new AppPreferences(ModPsswordActivity.this);
        userid = pref.getString("userid", "");
    }

    @OnClick(R.id.yes)
    public void onViewClicked() {
        if (oldPassword.getText().toString() == null || oldPassword.getText().toString().equals("")) {
            ToastUtil.show(ModPsswordActivity.this, "旧密码不能为空");
            return;
        }
        if (newPassword.getText().toString() == null || newPassword.getText().toString().equals("")) {
            ToastUtil.show(ModPsswordActivity.this, "新密码不能为空");
            return;
        }
        if (conPwd.getText().toString() == null || conPwd.getText().toString().equals("")) {
            ToastUtil.show(ModPsswordActivity.this, "确认密码不能为空");
            return;
        }
        if(!newPassword.getText().toString().equals(conPwd.getText().toString())){
            ToastUtil.show(ModPsswordActivity.this, "新密码必须和确认密码保持一致");
            return;
        }
        RequestParams params = new RequestParams(Config.SERVER_URL + "sys/loginapi/updatePwd");
        params.addBodyParameter("id", userid);
        params.addBodyParameter("oldPassword", String.valueOf(oldPassword.getText()));
        params.addBodyParameter("newPassword", String.valueOf(newPassword.getText()));
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (result.equals("success")) {
                    ToastUtil.show(ModPsswordActivity.this, "修改成功");
                    finish();

                } else if(result.equals("different")){
                    ToastUtil.show(ModPsswordActivity.this, "原密码错误");

                }else{
                    ToastUtil.show(ModPsswordActivity.this, "修改失败");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }
}
